/**
 * Created by sxwu on 14-2-14.
 */
/**
 * Module dependencies.
 */
//需要依赖的包设置
var express = require('express');


var http = require('http');
var path = require('path');
var ejs = require('ejs');
var app = express();
var config = require('./config');
// 设置所有变量及需要使用的变量定义
app.set('ip', config.static_host);//process.env.IP ||  "192.168.1.100");
app.set('port', config.static_port);//process.env.PORT || 8080);
app.set('views', path.join(__dirname, 'views'));
//使用html后缀，可以不使用ejs
app.engine('.html', ejs.__express);
app.set('view engine', 'html');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());

app.use(express.bodyParser({
    uploadDir: config.uploadPath
}));
app.use(express.methodOverride());
app.use(express.cookieParser('CLOUDSRV'));
app.use(express.session());
app.use(app.router); //必须放在这里，否则session无法保存
app.use(express.static(path.join(__dirname, 'public')));

// development only
if ('development' == app.get('env')) {
  app.use(express.errorHandler());
}

//用于微信接口
var weixin = require('./routes/weixin.js');
weixin.init(app);
/*
var wechat = require('wechat');
app.use('/weixin', wechat('CLOUDSRV',  function (req, res, next) {
    weixin.index(req, res);
}));*/


app.all('/public.html', function(req,res){res.render("public.html")});
//异步判断条件入口
var ajaxValid = require('./routes/ajaxValid.js');
app.all('/ajaxValid', ajaxValid.valid);
//注册企业用户
var register = require('./routes/register.js');
app.all('/register.html', register.register);

//重置密码
var forgetpwd = require('./routes/forgetpwd.js');
app.all('/forgetpwd.html', forgetpwd.index);
//企业用户管理
var useradmin = require('./routes/useradmin.js');
app.all('/useradmin.html', useradmin.index);
//////////////////////////////////////////////////////////////////////////
//设置路由,所有路由全部由index.js完成分发
//所有客户端的请求都在这里设置相应的分发路由
//////////////////////////////////////////////////////////////////////////
app.all('/oauth.html', function(req,res){res.render("oauth.html")});
//app.all('/wsmp', function(req,res){res.render("404.html",{errorno:req})});
///////////////////////////////////////////////////////////////////////////
app.all('/404.html', function(req,res){res.render("404.html")});
app.all('/useronlineinfo.html', function(req,res){res.render("useronlineinfo.html")});
//后台交互接口
/*AP首次注册*/
var deviceprc = require('./routes/deviceprc.js');
app.all('/', deviceprc.process);

/*AP上线认证*/
/*STA上线交互第一个接口        http://adwifi_diamon_ip:port/adwifi/?  */
var adwifi = require('./routes/wsmp.js');
app.all('/adwifi',adwifi.adwifi);

var oauth2 = require('./routes/oauth2.js');
app.all('/oauth2',oauth2.oauth2);

//////////////////////////////////////////////////////////////////////////
var login = require('./routes/login.js');
var index = require('./routes/index.js');
/* 登录页面 */
app.all('/login.html', login.login);

var index = require('./routes/index.js');
app.all('/index.html', index.index);

app.get('/logout', login.logout);

var modifypwd = require('./routes/modifypwd.js');
app.all('/modifypwd.html', modifypwd.index);

var operlog = require('./routes/operlog.js');
app.all('/operlog.html', operlog.index);
////////权限管理/////////////////////////////////////////////////////////
var roleadmin = require('./routes/roleadmin.js');
app.all('/roleadmin.html', roleadmin.index);

var roleinfo = require('./routes/roleinfo.js');
app.all('/roleinfo.html', roleinfo.index);

var useradmin = require('./routes/useradmin.js');
app.all('/useradmin.html', useradmin.index);

var userinfo = require('./routes/userinfo.js');
app.all('/userinfo.html', userinfo.index);
//////////////////////////////////////////////////////////////////////////
var notifyadmin = require('./routes/notifyadmin.js');
app.all('/notifyadmin.html', notifyadmin.index);

var notifyinfo = require('./routes/notifyinfo.js');
app.all('/notifyinfo.html', notifyinfo.index);
////////第一个菜单/////////////////////////////////////////////////////////
var wifiadmin = require('./routes/wifiadmin.js');
app.all('/wifiadmin.html', wifiadmin.index);
var wifiinfo = require('./routes/wifiinfo.js');
app.all('/wifiinfo.html', wifiinfo.index);


var setbindap = require('./routes/setbindap.js');
app.all('/setbindap.html', setbindap.index);
////////第二个菜单/////////////////////////////////////////////////////////
var adsmobile = require('./routes/adsmobile.js');
app.all('/adsmobile.html', adsmobile.index);

var adsother = require('./routes/adsother.js');
app.all('/adsother.html', adsother.index);
////////第三个菜单/////////////////////////////////////////////////////////
var querycustinfo = require('./routes/querycustinfo.js');
app.all('/querycustinfo.html', querycustinfo.index);

var querycustlog = require('./routes/querycustlog.js');
app.all('/querycustlog.html', querycustlog.index);
////////第四个菜单/////////////////////////////////////////////////////////
var statadvisit = require('./routes/statadvisit.js');
app.all('/statadvisit.html', statadvisit.index);

var statcustsrc = require('./routes/statcustsrc.js');
app.all('/statcustsrc.html', statcustsrc.index);

var getstatdata = require('./routes/getstatdata.js');
app.all('/getstatdata', getstatdata.index);
///////////////////////////////////////////////////////////////////////////

////////广告调用///////////////////////////////////////////////////////////
var adshow = require('./routes/adshow.js');
app.all('/adshow.html', adshow.index);
///////////////////////////////////////////////////////////////////////////
//启动web监听服务
//var config = require('./config');
// 创建服务端

//var crypto = require('crypto');
//console.log(crypto.createHash('md5').update("00:12:22:33:44:55adwifi").digest('hex').toLowerCase());

http.createServer(app).listen(app.get('port'), app.get('ip'), function() {


    console.log('+++++++++++++++++++++++++++++++++++++++++++++++++++');
    for(key in config) {
        if(!(typeof (config[key]) == "function")) {
            console.log(key + '\t\t= ' + config[key]);
        }
    }

    console.log('+++++++++++++++++++++++++++++++++++++++++++++++++++');
    console.log('启动服务器完成，IP: '+ app.get('ip') +', Web端口: ' + app.get('port'));
    console.log('+++++++++++++++++++++++++++++++++++++++++++++++++++');
});

